/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.dntuan.simple.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

/**
 * @author tuan.dang
 *
 * <br>$LastChangedRevision: 37 $
 * <br>$LastChangedDate: 2014-04-07 16:55:59 +0000 (Mon, 07 Apr 2014) $
 */
public class LogFilter implements Filter {
    private static final Logger LOGGER = Logger.getLogger(LogFilter.class.getName());
    FilterConfig filterConfig;

    public LogFilter() {
        LOGGER.info("Filter is initialized BEFORE container finishes the startup process BUT AFTER listeners are initialized");
    }

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        LOGGER.info("enter filter");
        LOGGER.info("request URI: " + httpRequest.getRequestURI());
        chain.doFilter(request, response);
        LOGGER.info("go out");
    }

    public void destroy() {
        this.filterConfig = null;
        LOGGER.info("filter is destroyed");
    }
}
